using System.Runtime.InteropServices;
using Microsoft.Web.WebView2.WinForms;

namespace ZhihuiApp;

[ClassInterface(ClassInterfaceType.AutoDual)]
[ComVisible(true)]
public class WebTool
{
    private MainForm _mainForm;
    private WebView2 _webView2;


    public WebTool(ref WebView2 webView2, MainForm mainForm)
    {
        _webView2 = webView2;
        _mainForm = mainForm;
    }

    public void Msg(string txt)
    {
        var s = $"JS发送来了消息:{txt}";
        Console.WriteLine(s);
        CallJs("777");
    }

    public void CallJs(object args)
    {
        var js = $"console.log({args} ,  '来自C#的调用')";
        _webView2.ExecuteScriptAsync(js);
    }


    /// <summary>
    ///     最小化主程序
    /// </summary>
    public void OnHide()
    {
        Console.WriteLine(@"JS改变窗体");
        _mainForm.OnChangeText("新标题来了");
 
    }
}